/**
 * Copyright (C) 2012 by team 4 M11CQK02-B (Khoa hoc may tinh 2011 - PTIT)
 * 
 * Advanced Algorithm Report
 */
package com.team4.AdvancedAlgorithm.alg;

/**
 * @author <a href="mailto:ndkhoi168@gmail.com">Nguyen Duc Khoi</a>
 * Jan 9, 2012
 */
public class Utils
{
   /**
    * Convert an integer number to 8 bits binary.
    * @param number
    * @return
    */
   public static String convertToBinary(int number)
   {
      String result = new String();
      int array[] = new int[8];
      for (int i = 0; i < 8; i++)
      {
         array[i] = 0;
      }
      int i = 0;
      while (number != 0)
      {
         array[i] = number % 2;
         number = number / 2;
         i++;
      }
      for (int j = 7; j >= 0; j--)
      {
         result += String.valueOf(array[j]);
      }
      return result;
   }

   public static String convertToIPDecimal(int binaryArr[])
   {
      String result = "";
      int start = 0;
      while (start < 32)
      {
         int arrs[] = new int[8];
         int j = 0;
         for (int i = start; i < start + 8; i++)
         {
            arrs[j++] = binaryArr[i];
         }
         if (start != 0)
         {
            result += ".";
         }
         result += convertToDecimal(arrs);
         start += 8;
      }

      return result;
   }

   private static String convertToDecimal(int arr[])
   {
      int t = 0;
      for (int i = 7; i >= 0; i--)
      {
         t += arr[i] * Math.pow(2, (7 - i));
      }

      return String.valueOf(t);
   }

   public static int calculateNetID(int X)
   {
      int netID = 0;
      if (X >= 0 && X <= 127)
         netID = 8;
      if (X >= 128 && X <= 191)
         netID = 16;
      if (X >= 192 && X <= 223)
         netID = 24;
      return netID;
   }

   public static String convertStringToIPAddress(String str)
   {
      if (str.length() != 32)
      {
         throw new IllegalArgumentException("Your IP Address has to contain 32 bit");
      }
      String result = "";
      for (int i = 0; i < str.length(); i++)
      {
         char a = str.charAt(i);
         if (i != 0 && i % 8 == 0)
         {
            result += ".";
         }
         result += a;
      }

      return result;
   }
   
   public static String convertToBinaryAddress(int x, int y, int z, int v)
   {
      String octet1 = Utils.convertToBinary(x);
      String octet2 = Utils.convertToBinary(y);
      String octet3 = Utils.convertToBinary(z);
      String octet4 = Utils.convertToBinary(v);
      return octet1 + "." + octet2 + "." + octet3 + "." + octet4;
   }

   public static boolean checkStringIsNumber(String str)
   {
      for (int i = 0; i < str.length(); i++)
      {
         if (!Character.isDigit(str.charAt(i)))
            return false;
      }
      return true;
   }

   public static boolean checkInvalidAddress(int x, int y, int z, int v)
   {
      if (invalidOctan(x) || invalidOctan(y) || invalidOctan(z) || invalidOctan(v))
      {
         return false;
      }
      return true;
   }

   public static boolean checkNetworkBit(int x, int m)
   {
      if (m < 0 || m > 30)
      {
         return false;
      }
      if (x >= 0 && x <= 127 && m <= 8)
         return false;
      if (x >= 128 && x <= 191 && m <= 16)
         return false;
      if (x >= 192 && x <= 223 && m <= 24)
         return false;
      return true;
   }

   private static boolean invalidOctan(int x)
   {
      if (x < 0 || x > 255)
         return true;
      return false;
   }

   public static String daoXau(String str)
   {
      String result = new String();
      String stemp[] = new String[4];
      for (int k = 0; k < 4; k++)
         stemp[k] = new String();
      int i = 0;
      int j = 0;
      while (i < str.length())
      {
         if (str.charAt(i) != '.')
         {
            stemp[j] += String.valueOf(str.charAt(i));
         }
         else
         {
            j++;
         }
         i++;
      }
      result = stemp[3] + "." + stemp[2] + "." + stemp[1] + "." + stemp[0];
      return result;
   }
}
